﻿using System.Collections.Generic;
using Turquoise.Business.Abstract;
using Turquoise.Entities.Data;
using Turquoise.Globalization;
using Turquoise.Helpers;
using Turquoise.Services.Messaging.Email.Microsoft;
using Turquoise.Services.Messaging.Entities;
using Turquoise.Services.Messaging.Helpers;
using Turquoise.Services.Messaging.Options;

namespace Turquoise.Business
{
    public class EmailManager : IEmailService
    {

        private readonly ISettingService _settingService;

        public EmailManager(ISettingService settingService)
        {
            _settingService = settingService;
        }

        public void Send(User user, string templateFilePath, TypeOptions emailTypes)
        {
            var templateText = FileHelper.ReadAllLines(templateFilePath);
            var eMailMessage = new EmailMessage();
            var from = new EmailAddress
            {
                DisplayName = _settingService.GetValueByKey("SmtpSenderName"),
                Address = _settingService.GetValueByKey("SmtpSenderMail")
            };

            eMailMessage.To = new List<EmailAddress>
            {
                new EmailAddress
                {
                    Address = user.Email
                }
            };
            var eMailSubject = "";
            var emailRow = new EmailRow();
            var emailKeys = new List<EmailKey>
            {
                new EmailKey
                {
                    Key = KeyOptions.ApplicationName,
                    Value = _settingService.GetValueByKey("ApplicationName")
                },
                new EmailKey
                {
                    Key = KeyOptions.ApplicationUrl,
                    Value = _settingService.GetValueByKey("ApplicationUrl")
                },
                new EmailKey
                {
                    Key = KeyOptions.Name,
                    Value = user.FullName
                }
            };

            switch (emailTypes)
            {
                case TypeOptions.Add:
                    {
                        eMailSubject = Resources.UserInformation;
                        emailKeys.Add(new EmailKey
                        {
                            Key = KeyOptions.Username,
                            Value = user.Username
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = KeyOptions.Password,
                            Value = user.Password
                        });
                        break;
                    }

                case TypeOptions.Activation:
                    {
                        eMailSubject = Resources.UserInformation;
                        emailKeys.Add(new EmailKey
                        {
                            Key = KeyOptions.Username,
                            Value = user.Username
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = KeyOptions.Password,
                            Value = user.Password
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = KeyOptions.ActivationCode,
                            Value = SecurityHelper.Encrypt(user.Id + "@" + user.CreateDate)
                        });
                        break;
                    }
                case TypeOptions.Update:
                    {
                        eMailSubject = Resources.UserInformation;
                        break;
                    }
                case TypeOptions.UpdatePassword:
                    {
                        eMailSubject = Resources.NewPassword;
                        emailKeys.Add(new EmailKey
                        {
                            Key = KeyOptions.Username,
                            Value = user.Username
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = KeyOptions.Password,
                            Value = user.Password
                        });
                        break;
                    }
                case TypeOptions.ForgotPassword:
                    {
                        eMailSubject = Resources.NewPassword;

                        emailKeys.Add(new EmailKey
                        {
                            Key = KeyOptions.Username,
                            Value = user.Username
                        });
                        emailKeys.Add(new EmailKey
                        {
                            Key = KeyOptions.Password,
                            Value = user.Password
                        });
                        break;
                    }
            }

            emailKeys.Add(new EmailKey
            {
                Key = KeyOptions.Subject,
                Value = eMailSubject
            });

            emailRow.EmailKeys = emailKeys;
            eMailMessage.Subject = eMailSubject;
            eMailMessage.From = from;
            eMailMessage.IsBodyHtml = true;
            eMailMessage.Priority = PriorityOptions.Normal;
            eMailMessage.Body = EmailHelper.ConvertTemplateToString(templateText, emailRow);
            var smtp = new Smtp(new TurquoiseSmtpClient
            {
                EnableSsl = _settingService.GetValueByKey("SmtpSsl").ConvertToBoolean(),
                Host = _settingService.GetValueByKey("SmtpServer"),
                Port = _settingService.GetValueByKey("SmtpPort").ConvertToInt(),
                Username = _settingService.GetValueByKey("SmtpUser"),
                Password = _settingService.GetValueByKey("SmtpPassword")
            });

            smtp.Send(eMailMessage);
        }
    }
}
